package com.healthbox.resultsservice.service.impl;

import com.healthbox.resultsservice.entity.CommonResult;
import com.healthbox.resultsservice.entity.HospitalNotice;
import com.healthbox.resultsservice.entity.HospitalNoticeForTOadminVue;
import com.healthbox.resultsservice.mapper.TOadminNoticesMapper;
import com.healthbox.resultsservice.service.TOadminNoticesService;
import com.healthbox.resultsservice.utils.MyTime;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class TOadminNoticesServiceImpl implements TOadminNoticesService {

    @Resource
    TOadminNoticesMapper tOadminNoticesMapper;

    @Override
    public CommonResult create(Map<String, Object> map, String userPhone, String userRole) {
        if (userRole == "user") {
            return new CommonResult<>(403, "权限不足");
        }
        HospitalNotice hospitalNotice = new HospitalNotice();
        hospitalNotice.setHospitalId(tOadminNoticesMapper.checkHospital(userPhone));
        hospitalNotice.setContent((String)map.get("content"));
        hospitalNotice.setTitle((String)map.get("title"));
        hospitalNotice.setTime(MyTime.getCurrTimeAsDate());
        int resCode = tOadminNoticesMapper.create(hospitalNotice);
        if (resCode != 1) {
            return new CommonResult<>(500, "创建失败");
        }
        return new CommonResult<>(200, "创建成功");
    }

    @Override
    public CommonResult delete(Map<String, Object> map, String userPhone, String userRole) {
        if (userRole == "user") {
            return new CommonResult<>(403, "权限不足");
        }
        Long noticeId = Long.valueOf(map.get("noticeId").toString());
        int resCode = tOadminNoticesMapper.delete(noticeId);
        if (resCode != 1) {
            return new CommonResult<>(500, "删除失败");
        }
        return new CommonResult<>(200, "删除成功");
    }

    @Override
    public CommonResult update(Map<String, Object> map, String userPhone, String userRole) {
        if (userRole == "user") {
            return new CommonResult<>(403, "权限不足");
        }
        HospitalNotice hospitalNotice = new HospitalNotice();
        hospitalNotice.setNoticeId(Long.valueOf(map.get("noticeId").toString()));
        hospitalNotice.setContent((String)map.get("content"));
        hospitalNotice.setTitle((String)map.get("title"));
        hospitalNotice.setTime(MyTime.getCurrTimeAsDate());
        int resCode = tOadminNoticesMapper.update(hospitalNotice);
        if (resCode != 1) {
            return new CommonResult<>(500, "修改失败");
        }
        return new CommonResult<>(200, "修改成功");
    }

    @Override
    public CommonResult retrieve(String userPhone, String userRole) {
        if (userRole == "user") {
            return new CommonResult<>(403, "权限不足");
        }
        Long hospital_id = tOadminNoticesMapper.checkHospital(userPhone);
        List<HospitalNotice> origin = tOadminNoticesMapper.retrieve(hospital_id);
        List<HospitalNoticeForTOadminVue> res = new ArrayList<>();
        for (HospitalNotice item: origin) {
            HospitalNoticeForTOadminVue hospitalNoticeForTOadminVue = new HospitalNoticeForTOadminVue();
            hospitalNoticeForTOadminVue.setNoticeId(item.getNoticeId());
            hospitalNoticeForTOadminVue.setContent(item.getContent());
            hospitalNoticeForTOadminVue.setTitle(item.getTitle());
            hospitalNoticeForTOadminVue.setTime(MyTime.dateTime2String(item.getTime()));
            res.add(hospitalNoticeForTOadminVue);
        }
        if (res.size() >= 1) {
            return new CommonResult<>(200, "查询成功", res);
        } else {
            return new CommonResult<>(200, "暂无公告", res);
        }
    }
}
